//
//  LzwUserMyCollectVc.m
//  NewHalfsugar
//
//  Created by 刘志武 on 16/1/13.
//  Copyright © 2016年 刘志武. All rights reserved.
//

#import "LzwUserMyCollectVc.h"
#import "LzwSingleton.h"
#import "LzwbaseTableView.h"
#import "LzwAdressModle.h"
#import "LzwUserCollectWebVcew.h"

@interface LzwUserMyCollectVc ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, retain) LzwbaseTableView *MyCollectionTableView;

@end

@implementation LzwUserMyCollectVc

-(void)dealloc{
    [_MyCollectionTableView release];
    [super dealloc];
}

//进行刷新,因为你不刷新的话的点出去重新进来才会有.
- (void)viewWillAppear:(BOOL)animated{
    
//    [super viewWillAppear:YES];
    
    [self.MyCollectionTableView reloadData];
    
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    

    self.navigationItem.title = @"我的收藏";
    
    UIImage *image = [UIImage imageNamed:@"return.png"];
    image = [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithImage:image style:UIBarButtonItemStylePlain target:self action:@selector(LeftButtonAction:)];
    
    self.MyCollectionTableView = [[LzwbaseTableView alloc]initWithFrame:[[UIScreen mainScreen]bounds]];
    [self.view addSubview:self.MyCollectionTableView];
    
    [self.MyCollectionTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
    
    self.MyCollectionTableView.dataSource = self;
    self.MyCollectionTableView.delegate = self;
    
    

    
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return [[LzwSingleton shareDataBase] selectBellTable].count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    NSMutableArray *array = [[LzwSingleton shareDataBase] selectBellTable];
    NSLog(@"ffffff = %@", array);
    
    LzwAdressModle *modleA = [array objectAtIndex:indexPath.row];
    
    cell.textLabel.textColor = [UIColor lightGrayColor];
    
    cell.textLabel.text = modleA.title;


    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    LzwUserCollectWebVcew *webVc = [[LzwUserCollectWebVcew alloc]init];
    
    NSMutableArray *array = [[LzwSingleton shareDataBase] selectBellTable];

    LzwAdressModle *modleA = [array objectAtIndex:indexPath.row];
    
    webVc.string = modleA.extend;

    [self.navigationController pushViewController:webVc animated:YES];
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    //先从数据库中把数据取出来 删掉, 然后在删除cell上面的数据
    
    NSMutableArray *array = [[LzwSingleton shareDataBase]selectBellTable];
    
  LzwAdressModle *modle = [array objectAtIndex:indexPath.row];
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        
        [[LzwSingleton shareDataBase] deleteBellCollect:modle.title];
        
        [array removeObjectAtIndex:indexPath.row];
        
        
        //删除cell
        
        [tableView deleteRowsAtIndexPaths:[NSMutableArray arrayWithObjects:indexPath, nil] withRowAnimation:UITableViewRowAnimationTop];
        
        
    }
    

    
}



- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 60;
}

- (void)LeftButtonAction:(UIBarButtonItem *)bar{
    [self.navigationController popViewControllerAnimated:YES];
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
